package com.loong.musicdemo.ui.find.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.SongList;
import com.loong.musicdemo.ui.find.activity.ShowAlbumPlayListActivity;

import java.util.ArrayList;
import java.util.List;

public class SearchPlaylistAdapter extends RecyclerView.Adapter<SearchPlaylistAdapter.ViewHolder> {
    private final RequestOptions requestOptions;
    private List<SongList> songLists;
    private Context context;

    public SearchPlaylistAdapter(List<SongList> songLists, Context context) {
        this.songLists = songLists;
        this.context = context;
        requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.temp);
        requestOptions.circleCropTransform();
        requestOptions.bitmapTransform( new RoundedCorners(10));
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_playlist_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SongList s = songLists.get(position);
        holder.search_playlist_name.setText(s.getName());
        holder.search_playlist_trackCount.setText(s.getTrackCount()+"首");
        holder.search_playlist_info.setText(s.getCreatorName()+"，播放"+s.getPlayCount()+"次");
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getAdapterPosition();
                SongList ss = songLists.get(pos);
                Intent songIntent = new Intent(context, ShowAlbumPlayListActivity.class);
                songIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                songIntent.putExtra("albumName", ss.getName());
                songIntent.putExtra("albumImg", ss.getBackgroundUrl());
                songIntent.putExtra("albumIDs", (ArrayList) ss.getSongIds());
                songIntent.putExtra("albumCreatorName", ss.getCreatorName());
                songIntent.putExtra("albumCreatorPic", ss.getCreatorAvatarUrl());
                songIntent.putExtra("albumCommentCount", ss.getCommentCount());
                songIntent.putExtra("albumPlayCount", ss.getPlayCount());
                songIntent.putExtra("albumId", ss.getSongListId());
                context.startActivity(songIntent);
            }
        });
        Glide.with(context).load(s.getBackgroundUrl()).apply(requestOptions).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(holder.search_playlist_image);
    }

    @Override
    public int getItemCount() {
        return songLists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView search_playlist_image;
        TextView search_playlist_name,search_playlist_trackCount,search_playlist_info;
        View view;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            search_playlist_image = itemView.findViewById(R.id.search_playlist_image);
            search_playlist_name = itemView.findViewById(R.id.search_playlist_name);
            search_playlist_trackCount = itemView.findViewById(R.id.search_playlist_trackCount);
            search_playlist_info = itemView.findViewById(R.id.search_playlist_info);
        }
    }
}
